%Name:		Xu Zelin 许泽林
%Course:	Digital Content Security
%Project：  F5(attack)

%% Prepare
img_path_list=dir('./image/*.jpg');
img_num = length(img_path_list);%获取图像总数量
filepath=pwd;
mkdir 简单攻击--压缩
mkdir 简单攻击--噪声（高斯噪声）
mkdir 简单攻击--噪声（椒盐噪声）
mkdir 简单攻击--噪声（泊松噪声）
mkdir 简单攻击--噪声（乘性噪声）
mkdir 同步攻击--缩放
mkdir 同步攻击--旋转

%% Attack
% 针对所有图片进行攻击
if img_num > 0 %有满足条件的图像
    fprintf('开始处理了');
    for j = 1:img_num %逐一读取图像
        figure(1);
        image_name = img_path_list(j).name;% 图像名
        image =  imread(strcat('./image/',image_name));
        fprintf('%d %s\n',j,image_name);% 显示正在处理的图像名
        subplot(6,6,j)
        imshow(image,[])
       %% 简单攻击--压缩
        figure(2);
        cr = 0.25;
        i1 = im2double(image);
        %对图像进行哈达玛变换
        t = dctmtx(8);%生成一个8*8 DCT变换矩阵
        dctcoe = blkproc(i1,[8 8],'P1*x*P2',t,t');%将图像分割为8*8的子图像进行FFT
        %x就是每一个分成的8*8大小的块，P1*x*P2相当于像素块的处理函数，p1 = T p2 = T’,
        %也就是fun = p1*x*p2' = T*x*T'的功能是进行离散余弦变换
        coevar = im2col(dctcoe,[8 8],'distinct');%降变换系数矩阵重新排列
        coe = coevar;
        [y,ind] = sort(coevar);
        [m,n] = size(coevar);%根据压缩比确定要变0的系数个数
        snum = 64 - 64 * cr;%舍去不重要的系数
        for i = 1:n
            coe(ind(1:snum),i) = 0;%将最小的snum个变换系数清0
        end
        b2 = col2im(coe,[8 8],[height(image) height(image)],'distinct');%重新排列系数矩阵
        %对截取后的变换系数进行哈达玛逆变换
        attack1 = blkproc(b2,[8 8],'P1*x*P2',t',t);%对截取后的变换系数进行哈达玛逆变换
        subplot(6,6,j)
        imshow(attack1,[])
        title('DCT变换图像');
        imwrite(attack1,strcat('./简单攻击--压缩/',image_name));
        
        %% 简单攻击--噪声（高斯噪声）
        figure(3)
        attack2=imnoise(image, 'gaussian', 0, 0.01);%方差为0.01的高斯噪声
        subplot(6,6,j);imshow(attack2,[])
        imwrite(attack2,strcat('./简单攻击--噪声（高斯噪声）/',image_name));
        figure(4)
        attack3=imnoise(image, 'gaussian', 0, 0.03);%方差为0.03的高斯噪声
        subplot(6,6,j);imshow(attack3,[])
        
        %% 简单攻击--噪声（椒盐噪声）
        figure(5)
        attack4=imnoise(image,'salt & pepper',0.01);%添加密度为0.01的椒盐噪声
        subplot(6,6,j);imshow(attack4,[])
        imwrite(attack4,strcat('./简单攻击--噪声（椒盐噪声）/',image_name));
        figure(6)
        attack5=imnoise(image,'salt & pepper',0.03);%添加密度为0.03的椒盐噪声
        subplot(6,6,j);imshow(attack5,[]) 

        %% 简单攻击--噪声（泊松噪声）
        figure(7)
        attack6=imnoise(image,'poisson');%%添加泊松噪声
        subplot(6,6,j);imshow(attack6,[])
        imwrite(attack6,strcat('./简单攻击--噪声（泊松噪声）/',image_name));

        %% 简单攻击--噪声（乘性噪声）
        figure(8)
        attack7=imnoise(image, 'speckle');%添加方差为0.04的乘性噪声
        subplot(6,6,j);imshow(attack7,[])
        imwrite(attack7,strcat('./简单攻击--噪声（乘性噪声）/',image_name));
        figure(9)
        attack8=imnoise(image,'speckle', 0.5);%添加方差为0.5的乘性噪声
        subplot(6,6,j);imshow(attack8,[]) 

        %% 同步攻击--缩放
        figure(10)
        attack9=imresize(image,2);
        subplot(6,6,j);imshow(attack9,[])
        imwrite(attack9,strcat('./同步攻击--缩放/',image_name));        
        figure(11)
        attack10=imresize(image,0.5);
        subplot(6,6,j);imshow(attack10,[])
        
        %% 同步攻击--旋转
        figure(12)
        attack11=imrotate(image,10,'bilinear','crop');   %最邻近线性插值算法旋转10度
        subplot(6,6,j),imshow(attack11),title('After Attack(最邻近线性插值算法旋转10度)');
        imwrite(attack11,strcat('./同步攻击--旋转/',image_name));                
              
    end
end

%% 针对一幅图片，经过压缩攻击后的图片再复原，与原图像相比的psnr（折线图）
image = imread('./image/5.1.12.jpg');
cr = [0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5];
mkdir radio_psnr_relationship
for j = 1:length(cr)
    i1 = im2double(image);
    t = dctmtx(8);
    dctcoe = blkproc(i1,[8 8],'P1*x*P2',t,t');
    coevar = im2col(dctcoe,[8 8],'distinct');
    coe = coevar;
    [y,ind] = sort(coevar);
    [m,n] = size(coevar);
    snum = 64 - 64 * cr;
    for i = 1:n
        coe(ind(1:snum),i) = 0;
    end
    b2 = col2im(coe,[8 8],[height(image) height(image)],'distinct');   
    attack1 = blkproc(b2,[8 8],'P1*x*P2',t',t);
end

        

